package com.zgs.mall.product.service.impl;

import com.zgs.mall.product.dao.AttrAttrgroupRelationDao;
import com.zgs.mall.product.entity.AttrAttrgroupRelationEntity;
import com.zgs.mall.product.entity.AttrEntity;
import com.zgs.mall.product.service.AttrService;
import com.zgs.mall.product.vo.AttrGroupVo;
import com.zgs.mall.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zgs.common.utils.PageUtils;
import com.zgs.common.utils.Query;

import com.zgs.mall.product.dao.AttrGroupDao;
import com.zgs.mall.product.entity.AttrGroupEntity;
import com.zgs.mall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    private AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String)params.get("key");
        //select * from pms_attr_group where catelog_id=catelogId and attr_group_id or attr_group_name
        QueryWrapper<AttrGroupEntity> wrapper=new QueryWrapper<AttrGroupEntity>();
        if (!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
        if (catelogId==0){
            IPage<AttrGroupEntity>  page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }else{
            wrapper.eq("catelog_id",catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrGroupVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //查询出该分类下所有属性分组
        List<AttrGroupEntity> attrGroupList = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupVo> collect = attrGroupList.stream().map(attrGroup -> {
            AttrGroupVo groupVo = new AttrGroupVo();
            BeanUtils.copyProperties(attrGroup, groupVo);
            Long attrGroupId = attrGroup.getAttrGroupId();
            List<AttrEntity> relation = attrService.getRelation(attrGroupId);
            groupVo.setAttrs(relation);
            return groupVo;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long catalogId, Long spuId) {
        List<SpuItemAttrGroupVo> list=  this.baseMapper.getAttrGroupWithAttrsBySpuId( catalogId,  spuId);
        return list;
    }


}